﻿/// <summary>
/// <Filename>: Motion_Picture.cs
/// Author: Jiang Xiaolong
/// Created: 2015.01.30
/// Version: 1.0
/// Company: Sunnytech
/// Function: Picture表格运动信息处理类
///
/// Changed By:
/// Modification Time:
/// Discription:
/// <summary>
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Data;
using System.Collections;
using Motion.MotionPlatform.Tools;
using System.Reflection;

namespace Motion.MotionPlatform
{
	public class PictureMotion : IMotion
	{
		//默认为Active
		public CurrentState State
		{
			get { return _state; }
			set { _state = value; }
		}
		private CurrentState _state = CurrentState.Active;
		//时间参数
		//正常速率时间
		public float StandardTime
		{
			get { return _standardTime; }
			set { _standardTime = value; }
		}
		private float _standardTime = 0f;
		//运动开始时间
		public float StartTime
		{
			get { return _startTime; }
			set { _startTime = value; }
		}
		private float _startTime = 0f;
		//运动结束时间
		public float EndTime
		{
			get { return _endTime; }
			set { _endTime = value; }
		}
		private float _endTime = 0f;

		//速率
		public float SpeedRate
		{
			get { return Motion_Data.SpeedRate; }
			set { Motion_Data.SpeedRate = value; }
		}
		//暂停状态
		public bool PauseControl
		{
			get { return Motion_Data.PauseControl; }
			set { Motion_Data.PauseControl = value; }
		}

		public string PictureName = "";
		public PictureDisplay Display = PictureDisplay.Motion;
		public bool NeedDispear = true;

		//结构初始化
		public void Init()
		{
			Type _t = Motion_TypeGet.MotionTypeGet("ReflectionManager");
			if (!MotionPlatformData.st_ReflectionManager && _t != null)
			{
				MotionPlatformData.st_ReflectionManager = (MonoBehaviour)GameObject.FindObjectOfType(_t);
			}
			if (MotionPlatformData.st_ReflectionManager && _t != null)
			{
				MethodInfo methodInfo = _t.GetMethod("ReflectionPictureValid");
				BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
				object[] para = new object[] { PictureName };
				methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
			}
		}

		//暂停控制
		public void Pause(bool play_status)
		{

		}

		//速率控制
		public void ChangeRate(float current_rate, float current_time)
		{
			EndTime = current_time + (EndTime - current_time) * SpeedRate / current_rate;
		}

		//运动
		public void Move(float current_time, float speed_rate, float delta_time)
		{

		}

		//时间管理
		public bool TimesUp(float current_time)
		{
			if (EndTime == 0f)
				return true;
			else if (current_time > EndTime)
				return true;
			else
				return false;
		}

		//后处理，主要是位置修正
		public void PostProcess()
		{
			if (Display == PictureDisplay.Time && NeedDispear)
			{
				Type _t = Motion_TypeGet.MotionTypeGet("ReflectionManager");
				if (!MotionPlatformData.st_ReflectionManager && _t != null)
				{
					MotionPlatformData.st_ReflectionManager = (MonoBehaviour)GameObject.FindObjectOfType(_t);
				}
				if (MotionPlatformData.st_ReflectionManager && _t != null)
				{
					MethodInfo methodInfo = _t.GetMethod("ReflectionPictureInvalid");
					BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
					object[] para = new object[] { PictureName };
					methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
				}
			}
		}

	}


	/// <summary>
	/// Picture运动信息提取管理类，相当于工厂；
	/// </summary>
	public class Motion_PictureInfoManager : IMotionInfoManager
	{
		/// <summary>
		/// 信息获取函数，用反射的方式创建Tips相应的解析类；
		/// </summary>
		/// <param name="motion_symbol">当前运动标识符；</param>
		/// <param name="data_row">当前传入的Excel数据行；</param>
		/// <param name="row_id">当前行对应ID号；</param>
		/// <param name="group_table">当前Group数据表；</param>
		/// <param name="motion_list">简单运动类列表；</param>
		/// <param name="complex_motion_list">复合运动类列表；</param>
		/// <returns>解析是否正确；</returns>
		public bool InfoGet(string motion_symbol, int state, DataRow data_row, string row_id, DataTable group_table, IList<IMotion> motion_list, IList<IList<IMotion>> complex_motion_list)
		{
			string motionTypeID = "ONLY";
			if (Motion_Data.MotionFunctionAllow(motion_symbol, motionTypeID, state))
			{
				if (Motion_Data.MotionFunctionClassDic[motion_symbol].ContainsKey(motionTypeID))
				{
					Type _t = Motion_TypeGet.MotionTypeGet("Motion.MotionPlatform." + Motion_Data.MotionFunctionClassDic[motion_symbol][motionTypeID]);
					//实例化相应的解析类
					IMotionFunctionInfo infoInstance = (IMotionFunctionInfo)Activator.CreateInstance(_t);

					////将Excel中参数转换成参数表里面的数据
					//Motion_Compute.ExcelDataAnalysis(ref data_row);
					return infoInstance.InfoGet(data_row, row_id, group_table, motion_list, complex_motion_list);
				}
				else
				{
					Debug.LogError("Picture运动标志符：" + motionTypeID + "，不含有该运动标志符对应的解析类，请询问相关程序猿！");
					return false;
				}
			}
			else
			{
				return true;
			}
		}
	}


	/// <summary>
	/// Picture运动；
	/// </summary>
	public class Motion_Picture_Only : IMotionFunctionInfo
	{
		public bool InfoGet(DataRow data_row, string row_id, DataTable group_table, IList<IMotion> motion_list, IList<IList<IMotion>> complex_motion_list)
		{
			PictureMotion _pictureMotion = new PictureMotion();

			bool is_right = true;

			Hashtable pictureHashTable = new Hashtable();

			//加载图片名字
			string picName = (string)data_row[2].ToString();
			//图片缩放比率
			string rateStr = (string)data_row[3].ToString();
			float picRate = 1f;
			if (rateStr != "")
			{
				picRate = Motion_Compute.FloatConversion(rateStr, ref is_right);
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("PICTURE", "RATE", row_id) + "，图片缩放比例格式填写错误！");
					}
					return false;
				}
			}
			//图片检查
			Type _t = Motion_TypeGet.MotionTypeGet("ReflectionManager");
			if (!MotionPlatformData.st_ReflectionManager && _t != null)
			{
				MotionPlatformData.st_ReflectionManager = (MonoBehaviour)GameObject.FindObjectOfType(_t);
			}
			if (MotionPlatformData.st_ReflectionManager && _t != null)
			{
				MethodInfo methodInfo = _t.GetMethod("ReflectionPicCheck");
				BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
				object[] para = new object[] { picName, picRate };
				object picVal = methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
				is_right = (bool)picVal;
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("PICTURE", "NAME", row_id) + "，图片文件夹中无名字为" + picName + "的图片！");
					}
					return false;
				}
			}
			_pictureMotion.PictureName = picName;

			if (MotionPlatformData.st_ReflectionManager && _t != null)
			{
				MethodInfo methodInfo = _t.GetMethod("ReflectionPicSetRate");
				BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
				object[] para = new object[] { picName, picRate };
				methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
			}

			//是否跟随Motion运动停止
			string motionStr = (string)data_row[4].ToString();
			if (motionStr.ToLower() == "true")
			{
				_pictureMotion.Display = PictureDisplay.Motion;
				_pictureMotion.NeedDispear = true;
				if (MotionPlatformData.st_ReflectionManager && _t != null)
				{
					MethodInfo methodInfo = _t.GetMethod("ReflectionPicAfterMotionAdd");
					BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
					object[] para = new object[] { picName };
					methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
				}

				pictureHashTable.Add("Show", false);
				//记录图片的状态
				if (Motion_Data.isEditor && MotionPlatformData.currentMotion == MotionPlatformState.Teaching)
				{
					if (MotionPlatformData.st_ReflectionManager && _t != null)
					{
						MethodInfo methodInfo = _t.GetMethod("ReflectionAddState");
						BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
						object[] para = new object[] { MotionPlatformData.excelName, MotionPlatformData.mainRowNumber + 2, "State_Picture", picName, pictureHashTable };
						methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
					}
				}
				motion_list.Add(_pictureMotion);
				return true;
			}
			else
			{
				_pictureMotion.Display = PictureDisplay.Time;
			}
			//时间信息获取
			string timeStr = (string)data_row[5].ToString();
			if (timeStr == "")
			{
				_pictureMotion.StandardTime = 0f;
				_pictureMotion.EndTime = 0f;
			}
			else
			{
				_pictureMotion.StandardTime = Motion_Compute.FloatConversion(timeStr, ref is_right);
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("PICTURE", "TIME", row_id) + "，时间格式填写错误！");
					}
					return false;
				}
				_pictureMotion.EndTime = _pictureMotion.StandardTime / _pictureMotion.SpeedRate;
			}
			//是否消失判断
			string flagStr = (string)data_row[6].ToString();
			if (flagStr.ToLower() == "false")
			{
				_pictureMotion.NeedDispear = false;
			}
			else
			{
				_pictureMotion.NeedDispear = true;
			}

			pictureHashTable.Add("Show", !_pictureMotion.NeedDispear);
			//记录图片的状态
			if (Motion_Data.isEditor && MotionPlatformData.currentMotion == MotionPlatformState.Teaching)
			{
				if (MotionPlatformData.st_ReflectionManager && _t != null)
				{
					MethodInfo methodInfo = _t.GetMethod("ReflectionAddState");
					BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
					object[] para = new object[] { MotionPlatformData.excelName, MotionPlatformData.mainRowNumber + 2, "State_Picture", picName, pictureHashTable };
					methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
				}
			}

			motion_list.Add(_pictureMotion);

			return true;
		}
	}

}
